package com.lucweb.action;


import com.lucweb.entity.*;
import com.lucweb.service.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Controller("todayAction")
@Scope("prototype")
public class TodayAction extends ActionSupport{



    @Resource
    private TodayService todayService;



    @Resource
    private QuestionService questionService;

    @Resource
    private BaseDictService baseDictService;

    @Resource
    private JcService jcService;

    @Resource
    private WxService wxService;

    @Resource
    private WhyjService whyjService;

    @Resource
    private OtherService otherService;


    private TaskToday taskToday;

    private String exeDate;



    private int taskId;




    //新建今日任务
    public String newToday(){
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat  format = new SimpleDateFormat("MM/dd/yyyy");

        String todayDate =format.format(calendar.getTime());


        //把三种当日确认任务发送到newMission页面

        //获取检测
        List<TaskJc>taskJcList =jcService.showTaskByName(" isConfirm ='1' and executeTime='"+todayDate+"'");
        System.out.println("jcjcjcjcjc"+taskJcList);
        ActionContext.getContext().put("taskJcList",taskJcList);


        //获取维修

        List<TaskWx>taskWxList =wxService.showTaskByName(" isConfirm ='1' and executeTime='"+todayDate+"'");
        System.out.println("jcjcjcjcjc"+taskWxList);
        ActionContext.getContext().put("taskWxList",taskWxList);

        //获取维护月检测
        List<TaskWhyj>taskWhyjList =whyjService.showTaskByName(" isConfirm ='1' and executeTime='"+todayDate+"'");
        System.out.println("jcjcjcjcjc"+taskWhyjList);
        ActionContext.getContext().put("taskWhyjList",taskWhyjList);



        return "today";

    }

    //新建今日任务
    public String printToday(){
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat  format = new SimpleDateFormat("MM/dd/yyyy");

        String todayDate =format.format(calendar.getTime());


        //把三种当日确认任务发送到newMission页面

        //获取检测
        List<TaskJc>taskJcList =jcService.showTaskByName(" isConfirm ='1' and executeTime='"+todayDate+"'");
        System.out.println("jcjcjcjcjc"+taskJcList);
        ActionContext.getContext().put("taskJcList",taskJcList);

//        List<List<TaskQuestion>>questions=new ArrayList<List<TaskQuestion>>();
//        for(int i=0;i<taskJcList.size();i++){
//            String str=taskJcList.get(i).getCompanyName();
//            List<TaskQuestion>jcquestionList = questionService.getQuestion("companyName = '"+str+"' " +
//                    "and isfinish = '未处理' and type = '检测任务' and mustDo='1'");
//            questions.add(jcquestionList);
//        }
//        ActionContext.getContext().put("questions",questions);





        //获取维修

        List<TaskWx>taskWxList =wxService.showTaskByName(" isConfirm ='1' and executeTime='"+todayDate+"'");
        System.out.println("jcjcjcjcjc"+taskWxList);
//        if(!taskWxList.isEmpty()){
//            TaskWx taskWx = taskWxList.get(0);
//            List<TaskQuestion>wxquestionList = questionService.getQuestion("companyName = '"+taskWx.getCompanyName()+"' " +
//                    "and isfinish = '未处理' and type = '维修任务' and mustDo='1'");
//            ActionContext.getContext().put("wxquestionList",wxquestionList);
//        }

        ActionContext.getContext().put("taskWxList",taskWxList);

        //获取维护月检测
        List<TaskWhyj>taskWhyjList =whyjService.showTaskByName(" isConfirm ='1' and executeTime='"+todayDate+"'");
        System.out.println("jcjcjcjcjc"+taskWhyjList);
        ActionContext.getContext().put("taskWhyjList",taskWhyjList);
        ActionContext.getContext().put("printdate",todayDate);
//        if(!taskWhyjList.isEmpty()){
//            TaskWhyj taskWhyj = taskWhyjList.get(0);
//            List<TaskQuestion>whyjquestionList = questionService.getQuestion("companyName = '"+taskWhyj.getCompanyName()+"' " +
//                    "and isfinish = '未处理' and type = '维护' and mustDo='1'");
//            ActionContext.getContext().put("whyjquestionList",whyjquestionList);
//        }


        return "todayPrint";

    }


    //新建今日任务
    public String prepareToday(){


        //把三种当日确认任务发送到newMission页面

        //获取检测
        List<TaskJc>taskJcList =jcService.showTaskByName("isConfirm ='0'");
        ActionContext.getContext().put("taskJcList",taskJcList);


        //获取维修

        List<TaskWx>taskWxList =wxService.showTaskByName("isConfirm ='0'");
        ActionContext.getContext().put("taskWxList",taskWxList);

        //获取维护月检测
        List<TaskWhyj>taskWhyjList =whyjService.showTaskByName("isConfirm ='0'");
        System.out.println("jcjcjcjcjc"+taskWhyjList);
        ActionContext.getContext().put("taskWhyjList",taskWhyjList);



        return "preparetoday";

    }


    //修改接电
    public String editToday(){

        taskToday = todayService.gettasktodayById(taskId);
        String todayDate=taskToday.getTodayDate();

        //把三种当日确认任务发送到newMission页面


        //获取检测
        List<TaskJc>taskJcList =jcService.showTaskByName("executeTime='"+todayDate+"'");
        System.out.println("jcjcjcjcjc"+taskJcList);
        ActionContext.getContext().put("taskJcList",taskJcList);


        //获取维修

        List<TaskWx>taskWxList =wxService.showTaskByName("executeTime='"+todayDate+"'");
        System.out.println("jcjcjcjcjc"+taskWxList);
        ActionContext.getContext().put("taskWxList",taskWxList);

        //获取维护月检测
        List<TaskWhyj>taskWhyjList =whyjService.showTaskByName("executeTime='"+todayDate+"'");
        System.out.println("jcjcjcjcjc"+taskWhyjList);
        ActionContext.getContext().put("taskWhyjList",taskWhyjList);

        ActionContext.getContext().put("taskToday",taskToday);



        return "today";

    }

    //删除
    public String deleteToday(){
        todayService.deletetodaytask(taskId);

        //刷新页面
        List<TaskToday>todayList=todayService.showtoday();
        ActionContext.getContext().put("todayList",todayList);

        return "todayIndex";
    }

    //打开安排任务列表
    public String taskshow(){

        //刷新页面
        List<String>taskList=todayService.taskshow();
        System.out.println(taskList);
        ActionContext.getContext().put("taskList",taskList);

        return "taskIndex";
    }

    //新建今日任务
    public String taskIndex(){



        //获取检测
        List<TaskJc>taskJcList =jcService.showTaskByName(" isConfirm ='1' and executeTime='"+exeDate+"'");
        System.out.println("jcjcjcjcjc"+taskJcList);
        ActionContext.getContext().put("taskJcList",taskJcList);


        //获取维修

        List<TaskWx>taskWxList =wxService.showTaskByName(" isConfirm ='1' and executeTime='"+exeDate+"'");
        System.out.println("jcjcjcjcjc"+taskWxList);
        ActionContext.getContext().put("taskWxList",taskWxList);

        //获取维护月检测
        List<TaskWhyj>taskWhyjList =whyjService.showTaskByName(" isConfirm ='1' and executeTime='"+exeDate+"'");
        System.out.println("jcjcjcjcjc"+taskWhyjList);
        ActionContext.getContext().put("taskWhyjList",taskWhyjList);
        ActionContext.getContext().put("exeDate",exeDate);




        return "taskDate";

    }


    //打印
    public String printTask(){


        //把三种当日确认任务发送到newMission页面

        //获取检测
        List<TaskJc>taskJcList =jcService.showTaskByName(" isConfirm ='1' and executeTime='"+exeDate+"'");
        System.out.println("jcjcjcjcjc"+taskJcList);
        ActionContext.getContext().put("taskJcList",taskJcList);


        //获取维修

        List<TaskWx>taskWxList =wxService.showTaskByName(" isConfirm ='1' and executeTime='"+exeDate+"'");
        System.out.println("jcjcjcjcjc"+taskWxList);
        ActionContext.getContext().put("taskWxList",taskWxList);

        //获取维护月检测
        List<TaskWhyj>taskWhyjList =whyjService.showTaskByName(" isConfirm ='1' and executeTime='"+exeDate+"'");
        System.out.println("jcjcjcjcjc"+taskWhyjList);
        ActionContext.getContext().put("taskWhyjList",taskWhyjList);

        ActionContext.getContext().put("printdate",exeDate);



        return "todayPrint";

    }





    //接电
    public String saveOrUpdate(){
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat  format = new SimpleDateFormat("MM/dd/yyyy");

        String todayDate =format.format(calendar.getTime());

        System.out.println("qwer"+taskToday.getTodayDate());
        if(taskToday.getTodayDate().length()==0){
            taskToday.setTodayDate(todayDate);
        }

       if(taskToday.getId()==0){
           int bid = todayService.getmaxId();
           taskToday.setId(bid);
       }


        //先保存
        todayService.saveOrUpdate(taskToday);

        //刷新页面
        List<TaskToday>todayList=todayService.showtoday();
        ActionContext.getContext().put("todayList",todayList);


        return "todayIndex";

    }

    //获取接电列表
    public String showToday(){


        List<TaskToday>todayList=todayService.showtoday();
        ActionContext.getContext().put("todayList",todayList);


        return "todayIndex";

    }

    public QuestionService getQuestionService() {
        return questionService;
    }

    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }
    public int getTaskId() {
        return taskId;
    }

    public void setTaskId(int taskId) {
        this.taskId = taskId;
    }
    public TodayService getTodayService() {
        return todayService;
    }

    public void setTodayService(TodayService todayService) {
        this.todayService = todayService;
    }

    public BaseDictService getBaseDictService() {
        return baseDictService;
    }

    public void setBaseDictService(BaseDictService baseDictService) {
        this.baseDictService = baseDictService;
    }


    public JcService getJcService() {
        return jcService;
    }

    public void setJcService(JcService jcService) {
        this.jcService = jcService;
    }

    public WxService getWxService() {
        return wxService;
    }

    public void setWxService(WxService wxService) {
        this.wxService = wxService;
    }

    public WhyjService getWhyjService() {
        return whyjService;
    }

    public void setWhyjService(WhyjService whyjService) {
        this.whyjService = whyjService;
    }

    public OtherService getOtherService() {
        return otherService;
    }

    public void setOtherService(OtherService otherService) {
        this.otherService = otherService;
    }

    public TaskToday getTaskToday() {
        return taskToday;
    }

    public void setTaskToday(TaskToday taskToday) {
        this.taskToday = taskToday;
    }

    public String getExeDate() {
        return exeDate;
    }

    public void setExeDate(String exeDate) {
        this.exeDate = exeDate;
    }
}
